package com.bdth.service.impl;

import com.bdth.model.BDMessage;
import com.bdth.model.TerminalCommand;
import com.bdth.model.TerminalCommandNew;
import com.bdth.service.ITerminalCommandService;
import com.bdth.utils.JacksonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

@Service
public class TerminalComandServiceImpl extends BaseServiceImpl<TerminalCommand> implements ITerminalCommandService {

	@Autowired
	private RedisTemplate redisTemplate;

	@Override
	public void sendTerminalCommand(TerminalCommand terminalCommand) throws Exception {

		//切换redis数据db
	/*	JedisConnectionFactory jedisConnectionFactory = (JedisConnectionFactory) redisTemplate.getConnectionFactory();
		jedisConnectionFactory.setDatabase(1);
		redisTemplate.setConnectionFactory(jedisConnectionFactory);*/
		ValueOperations<String, String> stringOperations = redisTemplate
				.opsForValue();
		stringOperations.set("SendTC_" + terminalCommand.getSimNo(), JacksonUtils.obj2json(terminalCommand));
		super.add(terminalCommand);
	}

	@Override
	public void sendBDMessage(BDMessage bdMessage) throws Exception {

		redisTemplate.opsForList().leftPush("MessageQueue", JacksonUtils.obj2json(bdMessage));
	}

	@Override
	public void sendCallTerminal(TerminalCommandNew terminalCommandNew) throws Exception {
		redisTemplate.opsForList().leftPush("CommandQueue", JacksonUtils.obj2json(terminalCommandNew));
	}
}
